window._=require('lodash');
try {
    window.$=window.jQuery=require('jquery');
    require('foundation-sites')
}catch(e){

}

window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

let csrfToken = document.head.querySelector('meta[name="csrf-token"]');
if (csrfToken) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = csrfToken.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
let BearerToken = window.localStorage.getItem('token');
if(BearerToken){
    window.axios.defaults.headers.common['Authorization'] = "Bearer "+BearerToken;
}


/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

//require('./bootstrap');

//window.Vue = require('vue');

//element-ui
//import ElementUi from 'element-ui';
//import 'element-ui/lib/theme-chalk/index.css'
//Vue.use(ElementUi);

import Vue from 'vue';
import router from './routes.js';
import store from "./store.js";
import registryToast from "./components/global/Toast";
import commonFunction from "./commonFunction/commonFunction";

Vue.use(registryToast);
Vue.prototype.myCommon = commonFunction;


new Vue({
    router,
    store,
    created(){
        //axios响应拦截器
        window.axios.interceptors.response.use(response=>{
            var token = response.headers.authorization;
            console.log('new token:'+token);
            if(token){
                window.axios.defaults.headers.common['Authorization'] = "Bearer "+token;
                this.$store.dispatch('refreshUserToken',{token:token});
            }
            return Promise.resolve(response);
        },error=>{
            console.log('error status code:'+error.response.status);
            return Promise.reject(error);
        });
    }
}).$mount('#app');


/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

// const files = require.context('./', true, /\.vue$/i)
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))

//Vue.component('example-component', require('./components/ExampleComponent.vue').default);

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

// const app = new Vue({
//     el: '#app',
// });
